package com.headwind.logistics.logistics.controller;

import com.headwind.logistics.common.constant.UserSessionKey;
import com.headwind.logistics.common.pojo.*;
import com.headwind.logistics.common.util.RestResult;
import com.headwind.logistics.common.util.ResultGenerator;
import com.headwind.logistics.logistics.pojo.LogiPreRecordedReceipt;
import com.headwind.logistics.logistics.service.intl.LogisticsCustomerServiceIntl;
import com.headwind.logistics.system.service.intl.GoodsTypeService;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @Author 小恶魔
 * @Date 2020-07-27 10:11
 * @Version 1.0
 * @DESCRIPTION 顾客前台首页控制器
 */
@Controller
@RequestMapping("/logistics/customer")
public class CustomerHomeController {

    //前台顾客业务逻辑层
    @Autowired
    private LogisticsCustomerServiceIntl logisticsCustomerServiceIntl;

    //api响应接口
    @Autowired
    private ResultGenerator resultGenerator;

    //货物类型业务逻辑层
    @Autowired
    private GoodsTypeService goodsTypeService;

    /**
     * 根据顾客id获取优惠卷
     * @param session
     * @return
     */
    @GetMapping("queryCouponByCustomerId")
    @ResponseBody
    public RestResult queryCouponByCusId(Integer type,HttpSession session){

        //从session中获取顾客对象
        Customer customer = (Customer) session.getAttribute(String.valueOf(UserSessionKey.CUSTOMER));

        List<Coupon> coupons = logisticsCustomerServiceIntl.queryCouponByCustomerId(type,customer.getId());

        return resultGenerator.getSuccessResult(coupons);
    }

    /**
     * 获取全部货物类型
     * @return
     */
    @GetMapping("queryAllGoodsType")
    @ResponseBody
    public RestResult queryAllGoodsType(){

        List<GoodsType> goodsTypes = goodsTypeService.selectGoodsType(null, null, null);

        return resultGenerator.getSuccessResult(goodsTypes);
    }

    /**
     * 根据顾客id获取全部的地址
     * @param session
     * @return
     */
    @GetMapping("queryAddressByCustomerId")
    @ResponseBody
    public RestResult queryAddressByCustomerId(HttpSession session,Integer page,Integer rows){

        Customer customer = (Customer) session.getAttribute(String.valueOf(UserSessionKey.CUSTOMER));

        List<Address> addresses = logisticsCustomerServiceIntl.queryAddressByCustomerId(customer.getId(),page,rows);
        //获取总页数
        int count = logisticsCustomerServiceIntl.queryAddressByCustomerId(customer.getId(), null, null).size();
        HashMap<String,Object> map = new HashMap<>();
        map.put("result",addresses);
        map.put("pageCount",count);
        return resultGenerator.getSuccessResult(map);

    }

    /**
     * 快递下单
     * @param putSendInfo
     * @param session
     * @return
     */
    @PostMapping("placeOrder")
    @ResponseBody
    public RestResult placeOrder(@RequestBody String putSendInfo
            ,HttpSession session){
        //获取预录单对象
        JSONObject jsonObject = JSONObject.fromObject(putSendInfo);
        jsonObject = JSONObject.fromObject(jsonObject.get("putSendInfo"));

        LogiPreRecordedReceipt preRecordedReceipt = (LogiPreRecordedReceipt)JSONObject.toBean(JSONObject.fromObject(jsonObject.get("preRecordedReceipt")),LogiPreRecordedReceipt.class);

        //获取顾客对象
        Customer customer = (Customer)session.getAttribute(String.valueOf(UserSessionKey.CUSTOMER));
        if (customer!=null){
            preRecordedReceipt.setCustomerId(customer.getId());
        }else{
            preRecordedReceipt.setCustomerId(0L);
        }

        Integer count = logisticsCustomerServiceIntl.addPreRecordedReceipt(putSendInfo, preRecordedReceipt);

        if (count >0){
            return resultGenerator.getSuccessResult();
        }

        return resultGenerator.getFailResult("提交失败了，再重新试试看？");
    }

}
